<?php include_once VIEWPATH.'common/dialog.php'; ?>
<script type="text/javascript">
    $(function($) {
        $(document).delegate("body", "click", function(e){
            $("#label_menu").hide();
        });
    });
    function on_submit(){
        var label_ids = $("#label_ids").val();
        if(!label_ids){
            alert("请选择标签");
            return false;
        }
        $('form').submit();
    }
    function getLabelList(){
        var label_name = $("#label_name").val();
        var url = 'index.php?c=label&m=label_search&label_name='+label_name;
        $.ajax({
            type:'post',
            url:url,
            data:{},
            cache:false,
            dataType:'json',
            success:function(data){
                if(data.errorCode == 0) {
                    var list = data.responseData;
                    if(list.length <=0 ){
                        $("#label_menu").html("");
                        $("#label_menu").show();
                        return;
                    }
                    var html = '';
                    for(var i = 0; i < list.length; i++){
                        html += '<li value="'+list[i]['id']+'" onclick="set_label_id(this)">'+list[i]['name']+'</li>';
                    }
                    $("#label_menu").html(html);
                    $("#label_menu").show();
                }else{
                    $("#label_menu").html("");
                    $("#label_menu").show();
                    return;
                }
            },
            error:function(data){
                return;
            }
        });
    }
    function set_label_id(obj){
        var id = $(obj).attr('value');
        var name = $(obj).html();

        var arr = {
            'id':id,
            'name':name
        };

        var label_ids = $("#label_ids").val();
        if(label_ids == '' || label_ids == null){
            label_ids = JSON.parse('[]');
        }else{
            label_ids = JSON.parse(label_ids);
        }
        for(var i = 0; i < label_ids.length; i++){
            if(id == label_ids[i]['id']){
                return false;
            }
        }
        var html = '<div class="item_info">';
        html += '<span class="close_d" onclick="del_label_html(this,'+id+')">x</span>';
        html += '<span>'+name+'</span>';
        html += '<div class="clearfix"></div>';
        html += '</div>';
        label_ids.push(arr);

        $("#label_ids").val(JSON.stringify(label_ids));
        $('#label_show').append(html);
    }
    function del_label_html(obj, uid){
        $(obj).parent().remove();
        var label_ids = $("#label_ids").val();
        label_ids = JSON.parse(label_ids);

        for(var i = 0; i < label_ids.length; i++){
            if(label_ids[i]['uid'] == uid){
                label_ids.splice(i,1);
            }
        }
        var label_ids_str = '';
        if(label_ids.length > 0){
            var label_ids_str = JSON.stringify(label_ids);
        }

        $("#label_ids").val(label_ids_str);
    }
    $("#label_add").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });
</script>

<div class="tab-pane active" id="horizontal-form">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true" aria-label="Close">×</button>
        <h5 class="modal-title"><?php echo $pageTitle;?></h5>
    </div>
    <form class="form-horizontal" name="form" id="form" action="index.php?c=<?php echo $_REQUEST['c'];?>&m=<?php echo $_REQUEST['m'];?>" name="form" method="post" enctype="multipart/form-data">
        <input type="hidden" name="fid_0"  id="fid_0" value="<?php echo $category_0["id"];?>"/>
        <input type="hidden" name="fid_1"  id="fid_1" value="<?php echo $category_1["id"];?>"/>
        <input type="hidden" name="submits" value="1"/>
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">标签类型</label>
            <div class="col-sm-2"><?php echo $category_0["name"];?></div>
        </div>
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">标签分组</label>
            <div class="col-sm-2"><?php echo $category_1["name"];?></div>
        </div>
        <div class="form-group">
            <label for="displayorder" class="col-sm-2 control-label">标签名称</label>
            <div class="col-sm-8">
                <div class="push-tag">
                    <div class="input-group suggests" style="margin-left:0px;width: 235px;">
                        <input type="hidden" name="label_ids" id="label_ids" value=''/>
                        <input type="text" placeholder="标签名称" name="label_name" class="input-sm form-control" id="label_name" value="" onkeyup="getLabelList()" AUTOCOMPLETE="OFF"/>
                        <span class="input-group-btn">
                           <button type="button" class="btn btn-sm btn-primary" onclick="getLabelList()"> 搜索</button>
                        </span>
                        <ul class="dropdown-menu dropdown-menu-right" id="label_menu"></ul>
                    </div>
                    <div class="clearfix"></div>
                    <div class="item_show" id="label_show"></div>
                </div>
            </div>
        </div>

        <div class="modal-footer">
            <button type="reset" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" onclick="on_submit()" class="btn btn-primary">保存</button>
        </div>

    </form>
</div>